package ee.ttu.rakarh.commands;

import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import ee.ttu.rakarh.model.data.dao.ItemTypeDAO;
import ee.ttu.rakarh.model.data.dao.ItemTypeDAOImpl;
import ee.ttu.rakarh.model.data.entities.Item;
import ee.ttu.rakarh.model.data.entities.ItemType;
import ee.ttu.rakarh.model.data.entities.TypeAttribute;
import ee.ttu.rakarh.services.ItemAttributeService;
import ee.ttu.rakarh.services.ItemService;
import ee.ttu.rakarh.services.ItemServiceImpl;

public class GetItemsAndTypes implements Command {

	private static final Logger LOG = Logger.getLogger(GetItemsAndTypes.class);
	
	@Override
	public Boolean execute(HttpServletRequest request, HttpServletResponse response) {
		
		Collection<Item> items = new ArrayList<Item>();
		Collection<TypeAttribute> attributes = new ArrayList<TypeAttribute>();
		ItemType type = new ItemType();
		type.setTypeName("");
		
		ItemTypeDAO itemTypeDAO = new ItemTypeDAOImpl();
		ItemService itemService = new ItemServiceImpl();
		ItemAttributeService itemAttrinuteService = new ItemAttributeService();
		
		try{
			String stringTypeId = request.getParameter("type");
			   if(stringTypeId != null){
			    int typeId = Integer.parseInt(stringTypeId);
			    items = itemService.getItemsByTypeId(typeId);
			    attributes = itemAttrinuteService.getTypeAttributesByTypeId(typeId);
			    type = itemTypeDAO.getItemTypeById(typeId);
			   }
		}catch (Exception e) {
			LOG.error(e);
		}
		
		request.setAttribute("items", items);
		
			
		Collection<ItemType> itemTypes = new ArrayList<ItemType>();
		itemTypes = itemTypeDAO.getItemTypesByLevel(1);
		request.setAttribute("itemTypes", itemTypes);
		request.setAttribute("attributes", attributes);
		request.setAttribute("type", type);
		
		return true;
	}

}
